package com.button.boot.mail.service.impl;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.button.boot.mail.model.MailModel;
import com.button.boot.mail.service.MailService;

@Service
public class MailServiceImpl implements MailService {
	private static final Logger LOGGER = LoggerFactory.getLogger(MailServiceImpl.class);
	
	@Autowired
	private JavaMailSender javaMailSender;

	@Override
	public void sendSimpleMail(MailModel mailModel) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(mailModel.getFrom());
		message.setTo(mailModel.getTo());
		message.setSubject(mailModel.getSubject());
		message.setText(mailModel.getContent());
		try {
			javaMailSender.send(message);
		} catch (Exception e) {
			LOGGER.error("sendSimpleMail发送邮件时发生异常. ex={}", e);
		}
	}

	@Override
	public void sendHTMLMail(MailModel mailModel) {
		MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailModel.getFrom());
            helper.setTo(mailModel.getTo());
            helper.setSubject(mailModel.getSubject());
            helper.setText(mailModel.getContent(), true);

            javaMailSender.send(message);
            LOGGER.info("html邮件发送成功");
        } catch (Exception e) {
        	LOGGER.error("sendHTMLMail发送邮件时发生异. ex={}", e);
        }
	}
	
	@Override
    public void sendAttachmentMail(MailModel mailModel) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(mailModel.getFrom());
            mimeMessageHelper.setTo(mailModel.getTo());
            mimeMessageHelper.setSubject(mailModel.getSubject());
            mimeMessageHelper.setText(mailModel.getContent());
            //文件路径
            FileSystemResource resource = new FileSystemResource(new File(mailModel.getResourcePath()));
            String fileName = new File(mailModel.getResourcePath()).getName();
            mimeMessageHelper.addAttachment(fileName, resource);
            
            javaMailSender.send(mimeMailMessage);
        } catch (Exception e) {
        	LOGGER.error("sendAttachmentMail发送邮件时出现异常. ex={}", e);
        }
    }
	
	@Override
	public void sendInlineResourceMail(MailModel mailModel){
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailModel.getFrom());
            helper.setTo(mailModel.getTo());
            helper.setSubject(mailModel.getSubject());
            helper.setText(mailModel.getContent(), true);

            FileSystemResource res = new FileSystemResource(new File(mailModel.getResourcePath()));
            helper.addInline(mailModel.getResourceId(), res);

            javaMailSender.send(message);
        } catch (Exception e) {
        	LOGGER.error("sendInlineResourceMail发送邮件时出现异常. ex={}", e);
        }
    }
}
